//
//  HWyuyueBanViewController.m
//  HWanGame
//
//  Created by 陈道斌 on 2020/9/27.
//  Copyright © 2020 Chendaobin. All rights reserved.
//

#import "HWyuyueBanViewController.h"


#import "HWPanHangBangAllTableViewCell.h"
#import "HWTypeleibieCollectionViewCell.h"
#import "HWTypeleibieModel.h" // 模型
#import "HWPaiHangModel.h"

#import "HWHomezhongyaodeCell.h"



@interface HWyuyueBanViewController ()<UITableViewDataSource,UITableViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,HWHomezhongyaodeCellDelegate>
@property(strong,nonatomic)UITableView *mainaTableVie;
@property(strong,nonatomic) UIView *toubuhederView;

@property(strong,nonatomic)UILabel *renbangLabel; //热榜


@property(strong,nonatomic) UICollectionView *maincollectionView;
@property(strong,nonatomic) NSMutableArray *fenleiMuArr;

@property(strong,nonatomic) NSMutableArray *dataMuArr;
@property(strong,nonatomic) NSString *p;
@property(strong,nonatomic) NSString *max_p;
@property(strong,nonatomic) HWTypeleibieModel * seledeModel;

@end

@implementation HWyuyueBanViewController

-(UITableView *)mainaTableVie {

    if (_mainaTableVie == nil
        ) {

         _mainaTableVie = [[UITableView alloc]initWithFrame:CGRectMake(0,0, kScreenW,kScreenH-CF_NavHeight-CF_TabbarHeight-44-40) style:UITableViewStylePlain];
         _mainaTableVie.delegate = self;
         _mainaTableVie.dataSource = self;
         _mainaTableVie.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mainaTableVie.backgroundColor = [UIColor whiteColor];
        

    }
    return   _mainaTableVie ;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
  //  [self addAttribute];
    
    [self setupUI];
    [self addrefreshControl];
    //查询分类
  //   [self chaxunqueryClassification];
    
    
    
}

//// 查询分类
//- (void)chaxunqueryClassification {
//
//
//    NSDictionary *parametes = @{
//                                @"ct" :  @"app",
//                                @"ac" : @"fenlei",
//                                @"ios" : @"1"
//                                };
//
//  //  [MBProgressHUD show:<#(NSString *)#>]
//  //  [MBProgressHUD showMessage:@"正在加载" toView:nil];
//   // [MBProgressHUD show:@"正在加载..."];
//    self.dataMuArr = [[NSMutableArray alloc]init];
//
//    [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
//
//
//        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
//     //   [MBProgressHUD hideHUD];
//
//
//        if ([codeStr intValue] == 1) {
//
//          //  self.fenleiMuArr = [response[@"data"] mutableCopy];
//           //  [MBProgressHUD  showError:@"加载失败"];
//            self.fenleiMuArr  =  [HWTypeleibieModel mj_objectArrayWithKeyValuesArray:response[@"data"]];
//            HWTypeleibieModel *mdol = self.fenleiMuArr[0];
//            self.seledeModel = mdol;
//            mdol.xuazhong = YES;
//            [self.mainaTableVie.mj_header beginRefreshing];
//            [self.maincollectionView reloadData];
//
//
//
//        } else {
//
//             [MBProgressHUD  showError:@"加载失败"];
//
//        }
//        NSLog(@"%@",response);
//
//
//    } failure:^(NSString *error) {
//
//        [MBProgressHUD  showError:@"加载失败"];
//
//    }];
//
//
//
//}
//



#pragma  mark ---- 添加刷新

- (void)addrefreshControl {
    
    self.mainaTableVie.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(underRefresh)];
    
  [self.mainaTableVie.mj_header beginRefreshing];
    
 //     self.mainaTableVie.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(onRefresh)];
}

// 下拉刷新
- (void)underRefresh {
    
   
    
    self.p = @"1";
    
    
    
    self.dataMuArr = [[NSMutableArray alloc]init];
     NSString *userID = [HWLocallyStoredData getLoginUserID];
    
    if ([userID isEqualToString:@""]) {
        
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        
       //  [MBProgressHUD showError:@"你还没有登录那，无法查看是否预约"];
        return;
    }
    
    NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
    NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
    NSString *signMD5 = [WMUtil md5HexDigest:sign];
    NSDictionary *parametes = @{
                                    
                                       @"ct" : @"app",
                                       @"ac" : @"yuyue",
                                       @"ios" : @"1",
                                       @"uid": userID,
                                       @"t" :tStr,
                                       @"member_id":[HWLocallyStoredData getCode],
                                       @"sign": signMD5
                                       
                                       
                                       };
     
 //   [MBProgressHUD show:@""];
    [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
           
           // [SVProgressHUD dismiss];
           [self.mainaTableVie.mj_header endRefreshing];
           [self.mainaTableVie.mj_footer endRefreshing];
           
           
           NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
           
           if ([codeStr intValue] == 1) {
               
               

               self.dataMuArr = [HWGameListModel  mj_objectArrayWithKeyValuesArray:response[@"data"]];
               
               if (self.dataMuArr.count > 0){
                   
               } else {
                   [MBProgressHUD showError:@"暂时无数据"];
                   
               }
               self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
               self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
              
    
           } else {
               
                [MBProgressHUD showError:response[@"message"]];
               
               
           }
        
           [self.mainaTableVie reloadData];
           
       } failure:^(NSString *error) {
           [self.mainaTableVie.mj_header endRefreshing];
           [self.mainaTableVie.mj_footer endRefreshing];
           
           
       }];
    
    
}

// 上拉加载

- (void)onRefresh {
   
  
        
    if ([self.p intValue] > [self.max_p intValue]) {
        
        [MBProgressHUD showError:@"暂时无新数据，请稍后在加载"];
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        
        return;
    }
    
    
    
       NSDictionary *parametes = @{
                                       
                                            @"ct" : @"app",
                                            @"ac" : @"yuyue",
                                               @"ios" : @"1",
                                             @"member_id":[HWLocallyStoredData getCode],
            
                                            };
         
         [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
             
             // [SVProgressHUD dismiss];
             [self.mainaTableVie.mj_header endRefreshing];
             [self.mainaTableVie.mj_footer endRefreshing];
             
             
             NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
             
             if ([codeStr intValue] == 1) {
                 
                // self.dataMuArr = [HWKfModel  mj_objectArrayWithKeyValuesArray:response[@"data"]];
                 
                 int p = [self.p intValue];
                 
                 int max_p = [self.max_p intValue];
                 
                 if (p > max_p) {
                     [MBProgressHUD showError:@"没有更多数据，请待会在上拉加载"];
                     
                     return ;
                     
                 }
                 
                
                 NSMutableArray *mutableArr = [HWGameListModel  mj_objectArrayWithKeyValuesArray:response[@"data"]];
                 

                 for (int i = 0; i < mutableArr.count; i++) {
                     [self.dataMuArr addObject:mutableArr[i]];

                 }
                 
                 self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
                 self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
               
                 
      
             }
          
             [self.mainaTableVie reloadData];
             
         } failure:^(NSString *error) {
             [self.mainaTableVie.mj_header endRefreshing];
             [self.mainaTableVie.mj_footer endRefreshing];
             
             
         }];
    
    
    
    
}










- (void)setupUI {
    
   // self.view.backgroundColor = RGB(93, 30, 255);
    self.view.backgroundColor = [UIColor whiteColor];
    
   // [self addHeaderView];
    self.mainaTableVie.backgroundColor =  [UIColor whiteColor];
    [self.view addSubview:self.mainaTableVie];
   
  //   [self addhudongHederView];
    
    
}



//
- (void)addHeaderView  {
    
    
    UIView *hederview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 164+54)];
   // hederview.backgroundColor =  RGB(93, 30, 255);
    self.toubuhederView = hederview;
    
    hederview.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:hederview];
    
   // self.mainaTableVie.tableHeaderView = hederview;
    
    UIImageView *backimage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 164)];
    backimage.image = [UIImage imageNamed:@"ranking_bg1"];
    [hederview addSubview:backimage];
    
    self.renbangLabel = [[UILabel alloc]initWithFrame:CGRectMake(49, 61, kScreenW, 16)];
    self.renbangLabel.font = [UIFont systemFontOfSize:16];
    self.renbangLabel.textColor = RGB(255, 255, 255);
    self.renbangLabel.text = @"热游TOP榜";
    [hederview addSubview: self.renbangLabel];
    
    
    UIImageView *icoIamgeView = [[UIImageView alloc]initWithFrame:CGRectMake(kScreenW-62-52, 56, 62, 82)];
    icoIamgeView.image = [UIImage imageNamed:@"ranking_cup"];
    
    [hederview addSubview:icoIamgeView];
    
    
    UILabel    *jiehaoLabel = [[UILabel alloc]initWithFrame:CGRectMake(49, self.renbangLabel.bottom+11, kScreenW, 16)];
    
     CGSize size =   [WMUtil getWidthByString:@"根据下载量和玩家热度计算得出，关注热游排行掌握时下热门手游" withFont:[UIFont systemFontOfSize:12] withStringSize:CGSizeMake(kScreenW-62-52-10-49, 1000)];
    
     jiehaoLabel.frame = CGRectMake(49, self.renbangLabel.bottom+11, kScreenW-62-52-10-49, size.height);
    
    
    jiehaoLabel.font = [UIFont systemFontOfSize:12];
    jiehaoLabel.textColor = RGB(255, 255, 255);
    jiehaoLabel.text = @"根据下载量和玩家热度计算得出，关注热游排行掌握时下热门手游";
    jiehaoLabel.numberOfLines = 0;
    [hederview addSubview: jiehaoLabel];
    
    
    
}



//- (void)addhudongHederView  {
//
//     UIView *hederview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 40)];
//    // hederview.backgroundColor =  RGB(93, 30, 255);
//     self.toubuhederView = hederview;
//
//     hederview.backgroundColor = [UIColor whiteColor];
//     [self.view addSubview:hederview];
//
//
//
//    UIView *tupianView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 40)];
//    tupianView.backgroundColor = [UIColor redColor];
//     tupianView.backgroundColor = [UIColor whiteColor];
//    [self.toubuhederView  addSubview:tupianView];
//
//
//
//    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//    layout.scrollDirection  =  UICollectionViewScrollDirectionHorizontal;
//
//  //    layout.minimumLineSpacing = 18;
//
////      CGFloat juanju  = (kScreenW - 24*CFLScaleValue*2-62*CFLScaleValue*3-78*CFLScaleValue)/2;
//     // layout.minimumInteritemSpacing = 1;
//    layout.minimumLineSpacing = 10;
//   //   layout.headerReferenceSize = CGSizeMake(kScreenW, 142);  //设置headerView大小
//      self.maincollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(20, 0, kScreenW-20, 40) collectionViewLayout:layout];
//      // self.maincollectionView.showsHorizontalScrollIndicator = NO;
//      self.maincollectionView.delegate = self;
//      self.maincollectionView.dataSource = self;
//      self.maincollectionView.alwaysBounceVertical = NO;
//      //self.maincollectionView.pagingEnabled = YES;
//      self.maincollectionView.backgroundColor = [UIColor whiteColor];
//     self.maincollectionView.showsHorizontalScrollIndicator = NO;
//       self.maincollectionView.showsVerticalScrollIndicator = NO;
//
//     self.maincollectionView.contentInset =  UIEdgeInsetsMake(10, 0, 10, 0);
//
//   //   self.maincollectionView.contentInset = UIEdgeInsetsMake(0, 24*CFLScaleValue, 0, 24*CFLScaleValue);
//    //  [self.maincollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];  //  一定要设置
//      [self.maincollectionView registerClass:[HWTypeleibieCollectionViewCell class] forCellWithReuseIdentifier:@"cellID"];
//     //  [self.maincollectionView  registerNib:[UINib nibWithNibName:@"HWJinXuanCell" bundle:nil] forCellWithReuseIdentifier:@"cellID"];
//      self.maincollectionView.userInteractionEnabled = YES;
//      [tupianView   addSubview:self.maincollectionView];
//
//
//
//
//}





- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return  self.dataMuArr.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
//    static NSString *cellID = @"cellID";
//    HWPanHangBangAllTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
//    if (!cell ) {
//        cell  = [[HWPanHangBangAllTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
//    }
//    cell.backgroundColor = [UIColor whiteColor];
//    cell.selectionStyle =  UITableViewCellSelectionStyleNone;
//    cell.dataModel = self.dataMuArr[indexPath.row];
//
//
//    if (indexPath.row == 0) {
//        cell.rankingImageView.image = [UIImage imageNamed:@"panhangbandiyiming"];
//        cell.rankingImageView.hidden= NO;
//        cell.jiluLabel.hidden = YES;
//
//
//    } else if (indexPath.row == 1) {
//          cell.rankingImageView.image = [UIImage imageNamed:@"dierming2"];
//           cell.rankingImageView.hidden= NO;
//          cell.jiluLabel.hidden = YES;
//
//    } else if(indexPath.row == 2) {
//
//          cell.rankingImageView.image = [UIImage imageNamed:@"panhangdisan3"];
//           cell.rankingImageView.hidden= NO;
//          cell.jiluLabel.hidden = YES;
//
//    } else {
//
//           cell.rankingImageView.hidden = YES;
//           cell.jiluLabel.hidden = NO;
//           cell.jiluLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row+1];
//
//
//    }
    
    
    
    
     static NSString *cellID = @"cellID1";
        
        HWHomezhongyaodeCell  *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        
        
        if (!cell) {
            cell = [[NSBundle mainBundle]loadNibNamed:@"HWHomezhongyaodeCell" owner:nil options:nil].firstObject;
            
        }
    
    cell.delegete = self;
    
     cell.icowidth.constant = 52;
     cell.icoheight.constant = 52;
     cell.icotoplayout.constant = 10 ;
     cell.leftzuLaout.constant = 44;
    cell.nametopLayout.constant = 7;
    cell.baseheighelayout.constant = 62;
    
    
    if (indexPath.row == 0) {
         cell.rankingImageView.image = [UIImage imageNamed:@"panhangbandiyiming"];
         cell.rankingImageView.hidden= NO;
         cell.jiluLabel.hidden = YES;
         
         
     } else if (indexPath.row == 1) {
           cell.rankingImageView.image = [UIImage imageNamed:@"dierming2"];
            cell.rankingImageView.hidden= NO;
           cell.jiluLabel.hidden = YES;
         
     } else if(indexPath.row == 2) {
         
           cell.rankingImageView.image = [UIImage imageNamed:@"panhangdisan3"];
            cell.rankingImageView.hidden= NO;
           cell.jiluLabel.hidden = YES;
          
     } else {
         
            cell.rankingImageView.hidden = YES;
            cell.jiluLabel.hidden = NO;
            cell.jiluLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row+1];
         
         
     }
      
    
    
    
    
        HWGameListModel  *model = self.dataMuArr[indexPath.row];
        cell.yuyueModel = model;
        cell.xiantiaoView.hidden = YES;
     
       cell.newbeijingiamgeView.hidden = YES;
       cell.beijingshituView.backgroundColor = [UIColor whiteColor];
                   
       cell.jianjutupian.constant = 0;
       cell.topjianju.constant = 31;
    
 
    
     
     cell.contentView.backgroundColor  = RGB(255, 255, 255);
    cell.backgroundColor = [UIColor redColor];
    
     return  cell;
     
 
    
    return  cell;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    return  62;
    return   78;
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HWGameListModel   *model =  self.dataMuArr[indexPath.row];
    HWGameDetailController *detail = [[HWGameDetailController alloc]init];
    detail.gameId = model.id;
    [self.navigationController pushViewController:detail animated:YES];
    
    
}





#pragma mark --------- UICollectionViewDelegate


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
  //  5
    return self.fenleiMuArr.count;
}







- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HWTypeleibieCollectionViewCell *cell =  [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    HWTypeleibieModel  *model = self.fenleiMuArr[indexPath.row];
   //  cell.model = model;
    cell.reYouBanModel =  model;

    return cell;
    
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(42,20);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    DLog(@"12334");
    
    
       for (int i = 0; i < self.fenleiMuArr.count; i++) {
            HWTypeleibieModel *model  =   self.fenleiMuArr[i];
             model.xuazhong = NO;
       }
       HWTypeleibieModel *model  =   self.fenleiMuArr[indexPath.row];
       model.xuazhong = YES;
       [self.maincollectionView reloadData];
    
       self.seledeModel = model;
       [self.mainaTableVie.mj_header beginRefreshing];
       

       
       NSLog(@"点击了第%ld分item",(long)indexPath.item);
       
    
    
}




- (void)xuanzhongle:(HWGameListModel *)model {
    
    NSLog(@"uuuu");
    
//    if ([userID isEqualToString:@""]) {
//
//          [self.mainaTableVie.mj_header endRefreshing];
//          [self.mainaTableVie.mj_footer endRefreshing];
//
//         //  [MBProgressHUD showError:@"你还没有登录那，无法查看是否预约"];
//          return;
//      }
    
    
    
         NSString *userID = [HWLocallyStoredData getLoginUserID];
    
         if ([userID isEqualToString:@""]) {
             
            [MBProgressHUD showError:@"你还没有登录那,无法预约"];
             return;
          }
    
    
    
    
    
    
            NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
            NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
            NSString *signMD5 = [WMUtil md5HexDigest:sign];
            NSDictionary *parametes = @{
                                       @"ct" :  @"app",
                                       @"ac" : @"yuyue_add",
                                       @"ios" : @"1",
                                       @"uid" :userID,
                                       @"t" :tStr,
                                       @"gid": model.id,
                                       @"member_id":[HWLocallyStoredData getCode],
                                       @"sign": signMD5
                                       };
      
      
           
       //    [MBProgressHUD show:@"正在绑定邮箱"];
           
           [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
               
             
               NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
            //   [MBProgressHUD hideHUD];
               
               
               if ([codeStr intValue] == 1) {
                   
                
                    [self.mainaTableVie.mj_header beginRefreshing];
                  

               } else {
                
                    [MBProgressHUD  showSuccess:response[@"message"]];
                   
               }
               NSLog(@"%@",response);
               
               
           } failure:^(NSString *error) {

                  [MBProgressHUD  showError:@"查询失败"];
               
              
           }];
    
    
    
    
    
    
}

@end
